package de.jmda.home.ui.vaadin.navigation;

import com.google.common.eventbus.EventBus;
import com.vaadin.ui.Accordion;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TabSheet.SelectedTabChangeEvent;
import com.vaadin.ui.TabSheet.SelectedTabChangeListener;
import com.vaadin.ui.VerticalLayout;

import de.jmda.common.ui.web.vaadin.service.DefaultSessionContext;
import de.jmda.home.ui.vaadin.userguide.UserGuideChapterDisplayRequest;
import de.jmda.home.ui.vaadin.userguide.UserGuideDisplay.UserGuideChapter;
import de.jmda.home.ui.vaadin.userguide.UserGuideTree;

@SuppressWarnings("serial")
public class NavigationComponentController implements SelectedTabChangeListener
{
	private static enum TAB
	{
		HOME           ("home"),
		USER_GUIDE     ("user guide"),
		ARTICLES       ("articles"),
		TIPS_AND_TRICKS("tips and tricks"),
		FORUM          ("forum"),
		;

		private Component component;
		private String caption;
		
		private TAB(String caption)
		{
			this.caption = caption;
			
//			if (caption.equals(USER_GUIDE.caption))
			if (caption.equals("user guide"))
			{
				VerticalLayout verticalLayout = new VerticalLayout();
				
				component = verticalLayout;
				component.setSizeFull();

				new UserGuideTree(verticalLayout);
			}
			else
			{
				component = new Label(caption);
			}
		}

		public String getCaption()
		{
			return caption;
		}

		public Component getComponent()
		{
			return component;
		}
	}

	private Accordion accordion;

	public NavigationComponentController(Accordion accordion)
	{
		super();
		
		this.accordion = accordion;
		
		initAccordion();
	}

	private void initAccordion()
	{
		accordion.addTab(
				TAB.HOME.getComponent(),            TAB.HOME.getCaption());
		accordion.addTab(
				TAB.USER_GUIDE.getComponent(),      TAB.USER_GUIDE.getCaption());
		accordion.addTab(
				TAB.ARTICLES.getComponent(),        TAB.ARTICLES.getCaption());
		accordion.addTab(
				TAB.TIPS_AND_TRICKS.getComponent(), TAB.TIPS_AND_TRICKS.getCaption());
		accordion.addTab(
				TAB.FORUM.getComponent(),           TAB.FORUM.getCaption());
		
		accordion.addSelectedTabChangeListener(this);
	}

	@Override
  public void selectedTabChange(SelectedTabChangeEvent event)
  {
		String caption =
				accordion.getTab(event.getTabSheet().getSelectedTab()).getCaption();

		if (caption == null)
		{
			// accordion seems to be in initialisation phase
			return;
		}

		EventBus eventBus = DefaultSessionContext.get().getEventBus();

		if (caption.equals(TAB.HOME.getCaption()))
		{
			eventBus.post(new UserGuideChapterDisplayRequest(UserGuideChapter.HOME));
		}
		else if (caption.equals(TAB.USER_GUIDE.getCaption()))
		{
			eventBus.post(new UserGuideChapterDisplayRequest(UserGuideChapter.HOME));
		}
		else if (caption.equals(TAB.ARTICLES.getCaption()))
		{
			Notification.show("articles not yet implemented");
		}
		else if (caption.equals(TAB.TIPS_AND_TRICKS.getCaption()))
		{
			Notification.show("tips and tricks not yet implemented");
		}
		else if (caption.equals(TAB.FORUM.getCaption()))
		{
			Notification.show("forum not yet implemented");
		}
  }
}